package com.naiterui.ehp.ps.push.utils.umeng;

import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.stereotype.Component;

import com.naiterui.ehp.bp.utils.lang.ConfigUtil;
import com.naiterui.ehp.ps.push.utils.umeng.ios.IOSBroadcast;
import com.naiterui.ehp.ps.push.utils.umeng.ios.IOSGroupcast;
import com.naiterui.ehp.ps.push.utils.umeng.ios.IOSUnicast;

@Component
public class DoctorIosPusher {
    private String iosappkey = ConfigUtil.getString("doctor.ios.push.umeng.appkey");
    private String iosappMasterSecret = ConfigUtil.getString("doctor.ios.push.umeng.mastersecret");
    private static PushClient client = new PushClient();

    /**
     * ios 全量推送
     *
     * @throws Exception
     */
    public void sendIOSBroadcast(String text, String linkUrl) throws Exception {
        IOSBroadcast broadcast = new IOSBroadcast(iosappkey, iosappMasterSecret);
        //broadcast.setDescription(desc);
        broadcast.setAlert(text);
        broadcast.setBadge(1);
        broadcast.setSound("default");
        broadcast.setProductionMode();
        broadcast.setCustomizedField("custom", linkUrl);
        client.send(broadcast);
    }

    /**
     * ios 单个推送
     *
     * @throws Exception
     */
    public void sendIOSUnicast(String deviceToken, String text, String linkUrl) throws Exception {
        IOSUnicast unicast = new IOSUnicast(iosappkey, iosappMasterSecret);
        unicast.setDeviceToken(deviceToken);
        //unicast.setDescription(desc);
        unicast.setAlert(text);
        unicast.setBadge(1);
        unicast.setSound("default");
        unicast.setProductionMode();
        unicast.setCustomizedField("custom", linkUrl);
        client.send(unicast);
    }

    /**
     * ios 自定义标签推送
     *
     * @throws Exception
     */
    public void sendIOSGroupcast(String text, String linkUrl, List<String> tags) throws Exception {
        IOSGroupcast groupcast = new IOSGroupcast(iosappkey, iosappMasterSecret);
        /*
         *  Construct the filter condition:
         *  "where":
         *	{
         *		"and":
         *		[
         *			{"tag":"iostest"}
         *		]
         *	}
         */
        JSONObject filterJson = new JSONObject();
        JSONObject whereJson = new JSONObject();
        JSONArray tagArray = new JSONArray();
        tags.forEach(tag -> {
            JSONObject tagJson = new JSONObject();
            try {
                tagJson.put("tag", tag);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            tagArray.put(tagJson);
        });
        whereJson.put("and", tagArray);
        filterJson.put("where", whereJson);

        groupcast.setFilter(filterJson);
        //groupcast.setDescription(desc);
        groupcast.setAlert(text);
        groupcast.setBadge(0);
        groupcast.setSound("default");
        groupcast.setProductionMode();
        groupcast.setCustomizedField("custom", linkUrl);
        client.send(groupcast);
    }

    /*
    public void sendIOSCustomizedcast() throws Exception {
        IOSCustomizedcast customizedcast = new IOSCustomizedcast(iosappkey, iosappMasterSecret);
        // And if you have many alias, you can also upload a file containing these alias, then
        // use file_id to send customized notification.
        customizedcast.setAlias("alias", "alias_type");
        customizedcast.setAlert("IOS 个性化测试");
        customizedcast.setBadge(0);
        customizedcast.setSound("default");
        customizedcast.setTestMode();
        client.send(customizedcast);
    }*/

    /*
    public void sendIOSFilecast() throws Exception {
        IOSFilecast filecast = new IOSFilecast(iosappkey, iosappMasterSecret);
        String fileId = client.uploadContents(appkey, appMasterSecret, "aa" + "\n" + "bb");
        filecast.setFileId(fileId);
        filecast.setAlert("IOS 文件播测试");
        filecast.setBadge(0);
        filecast.setSound("default");
        filecast.setTestMode();
        client.send(filecast);
    }*/
}
